//
//  MapDetailController.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/10/18.
//

import UIKit
import MapKit
class MapDetailController: SecSuperController,MKMapViewDelegate {
    var appleMapView:MKMapView!
    var dic:[String:Any]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.gk_navTitle = LocalizedString("详情")
        
        
        let bottomView = UIView(frame: CGRect(x: 0, y: SCREEN_HEIGHT - pixw(p: 200), width: SCREEN_WIDTH, height: pixw(p: 220)))
        bottomView.backgroundColor = .white
        self.view.addSubview(bottomView)
        
        let nameLabel = UILabel()
        nameLabel.text = "新疆阿克苏地区库车市"
        nameLabel.font = MY_SCALE_SemiboldFONT(18)
        nameLabel.numberOfLines = 0
        nameLabel.lineBreakMode = .byWordWrapping
        nameLabel.textColor = MYBlock
        bottomView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 30))
            make.top.equalTo(pixw(p: 30))
            make.right.equalTo(pixw(p:-30))
        }
        
        let timeLabel = UILabel()
        timeLabel.text = "地震时间:2024-10-15 21:15:25"
        timeLabel.font = MY_SCALE_LightFONT(14)
        timeLabel.textColor = MYColor.color(withHexString: "#999999")
        bottomView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p:10))
        }
        
        let jingweiLabel = UILabel()
        jingweiLabel.text = "经度:83.98 纬度:40.91"
        jingweiLabel.font = MY_SCALE_FONT(14)
        jingweiLabel.textColor = MYColor.color(withHexString: "#999999")
        bottomView.addSubview(jingweiLabel)
        jingweiLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(timeLabel.snp.bottom).offset(pixw(p:10));
        }
        
        let shendiLabel = UILabel()
        shendiLabel.text = "深度:18千米"
        shendiLabel.font = MY_SCALE_FONT(14)
        shendiLabel.textColor = MYColor.color(withHexString: "#999999")
        bottomView.addSubview(shendiLabel)
        shendiLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(jingweiLabel.snp.bottom).offset(pixw(p:10))
        }
        
        let juliLabel = UILabel()
        juliLabel.text = "距你：0km"
        juliLabel.font = KHFONT(17)
        juliLabel.textColor = MYRed
        bottomView.addSubview(juliLabel)
        juliLabel.snp.makeConstraints { make in
            make.left.equalTo(shendiLabel);
            make.top.equalTo(shendiLabel.snp.bottom).offset(pixw(p: 10));
        }
        
        let dengjiLabel = UILabel()
        dengjiLabel.text = "3.2级"
        dengjiLabel.font = MY_SCALE_MediumFONT(19)
        dengjiLabel.textColor = MYColor.color(withHexString: "#fbb621")
        bottomView.addSubview(dengjiLabel)
        dengjiLabel.snp.makeConstraints { make in
            make.right.equalTo(pixw(p: -20));
            make.bottom.equalTo(juliLabel.snp.top).offset(pixw(p: -4));
        }
        
        nameLabel.text = dic!["LOCATION_C"] as? String ?? ""
        timeLabel.text = dic!["O_TIME"] as? String ?? ""
        jingweiLabel.text = "\(LocalizedString("经度")):\(dic!["EPI_LON"] ?? "")°   \(LocalizedString("纬度")):\(dic!["EPI_LAT"] as? String ?? "")°"
        shendiLabel.text = "\(LocalizedString("深度")):\(dic!["EPI_DEPTH"] ?? "")km"
        dengjiLabel.text = "\(dic!["M"] ?? "")级"
        //地震经纬度
        let dzLat = Double(dic!["EPI_LAT"] as! String) ?? 0.00
        let dzLon = Double(dic!["EPI_LON"] as! String) ?? 0.00
        //自己位置
        let zjLat = MYGlobal.share().locationModel.lat
        let zjLog = MYGlobal.share().locationModel.lon
    
        // 将CLLocationCoordinate2D结构转换为CLLocation对象
        let place1 = CLLocation(latitude: dzLat, longitude: dzLon)
        let place2 = CLLocation(latitude: zjLat, longitude: zjLog)
         
        // 使用distance(from:)方法计算两点间的距离
        let distance = place2.distance(from: place1) / 1000
        print("两点间的距离为: \(distance) 米")
        juliLabel.text = "\(LocalizedString("距您")):\(String(format: "%.2f", distance))km"
        
        
        
        self.appleMapView = MKMapView(frame: CGRect(x: 0, y: navigationFullHeight(), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - navigationFullHeight() - bottomView.height))
        self.appleMapView.delegate = self
        self.appleMapView.showsScale = true
        self.appleMapView.showsCompass = false
        self.appleMapView.mapType = .standard
        self.appleMapView.showsUserLocation = true
//        mapView.setZoomLevel(5, animated: true)
        self.view.addSubview(self.appleMapView)
        
        let location = CLLocationCoordinate2D(latitude: dzLat, longitude: dzLon)
        
        let pointAnnotation = MKPointAnnotation()
                       pointAnnotation.coordinate = location
                       pointAnnotation.title = "\(dic["M"] as? String ?? "")级"
                       pointAnnotation.subtitle = dic["LOCATION_C"] as? String ?? ""
                       self.appleMapView.addAnnotation(pointAnnotation)
                       self.appleMapView.selectAnnotation(pointAnnotation, animated: true)
        self.appleMapView.centerCoordinate = location
        
        let initialRegion = MKCoordinateRegion(
                   center: location,
                   span: MKCoordinateSpan(latitudeDelta: 0.8, longitudeDelta: 0.8))
                   self.appleMapView.region = initialRegion
        
        // 设置圆形范围
        let shendu = (Double(dic["EPI_DEPTH"] as? String ?? "00") ?? 0.00) * 1000
        let circle = MKCircle(center: location, radius: shendu)
        self.appleMapView.addOverlay(circle)
    }
}

extension MapDetailController{

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKPointAnnotation{
            let identifier = "CustomAnnotationView"
                   var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
                   if annotationView == nil {
                       annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                       annotationView?.canShowCallout = true

                       // 创建一个自定义按钮作为右侧的附属视图
                       let rightButton = UIButton(type:.custom)
                       annotationView?.rightCalloutAccessoryView = rightButton
                   } else {
                       annotationView?.annotation = annotation
                   }
                   return annotationView
            
        }
        return nil
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay.isKind(of: MKCircle.classForCoder()){
            let cirleRender = MKCircleRenderer(overlay: overlay)
            cirleRender.lineWidth = 2
            cirleRender.strokeColor = MYColor.color(withHexString: "#E04148", alpha: 0.2)
            cirleRender.fillColor = MYColor.color(withHexString: "#999999", alpha: 0.2)
            return cirleRender
        }
        return MKOverlayRenderer()
    }
}
